import json
import os

import pytest
import requests


@pytest.fixture(scope="session", autouse=True)
def setCookies():
    headers = {
        "content-type": "application/json;charset=utf-8"
    }
    url = "http://101.35.141.38:28019/api/v1/user/login"
    data = {
        "loginName": "17729597958",
        "passwordMd5": "e10adc3949ba59abbe56e057f20f883e"
    }
    res = requests.post(url=url, headers=headers, json=data)
    if res.status_code == 200:
        cookies = res.json()["data"]
        headers = {
            "content-type": "application/json;charset=utf-8",
            "token": cookies
        }
        # 登陆后获取cookies并且添加到请求头，写入到系统变量
        os.environ["headers"] = json.dumps(headers)
    else:
        raise RuntimeError("\n"+"responseException: "+str(res.json()))
